4749. Theater revenue

 

The theater has n rows with m seats. Two matrices are given. The first matrix contains the ticket prices for each seat. The second matrix tells which tickets are sold and which are not (1 means the ticket is sold for a corresponding seat, 0 is not sold).

Determine the total revenue from the performance.

 

Input. The first line contains n and m (n, m ≤ 500). Then the matrix with ticket prices is given (n lines with m numbers, each number ranges from 0 to 10000). Then given the matrix of sold tickets – n lines with m numbers.

 

Output. Print the total revenue from the ticket sales.

 

Sample input

Sample output

3 3

1 2 3

4 5 6

7 8 9

 

1 0 1

0 1 0

1 0 1

25

 

 

SOLUTION

two-dimensional array

 

Algorithm analysis

Read the matrix of ticket prices into a two-dimensional array c. Then read the matrix of sold tickets and compute the amount of revenue.

 

Example

Consider the sample given.

The total revenue is 1 + 3 + 5 + 7 + 9 = 25.

 

Algorithm realization

Declare a matrix of ticket prices.

 

#define MAX 501

int c[MAX][MAX];

 

Read the dimensions and the matrix of ticket prices.

 

scanf("%d %d",&n,&m);

for(i = 0; i < n; i++)

for(j = 0; j < m; j++)

  scanf("%d",&c[i][j]);

 

Read the matrix of sold tickets. If the ticket is sold (matrix element equals to 1) then add the ticket cost c[i][j] to the answer res.

 

res = 0;

for(i = 0; i < n; i++)

for(j = 0; j < m; j++)

{

  scanf("%d",&s);

  if (s == 1) res += c[i][j];

}

 

Print the cost of sold tickets.

 

printf("%d\n",res);

 

Java realization

 

import java.util.*;

//import java.io.*;

 

public class Main

{

  //public static void main(String[] args) throws IOException

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);   

    //Scanner con = new Scanner(new FileReader ("4749.in"));

    int i, j, n = con.nextInt(), m = con.nextInt();

    int c[][] = new int[n][m];   

    for(i = 0; i < n; i++)

    for(j = 0; j < m; j++)

      c[i][j] = con.nextInt();

   

    int res = 0;

    for(i = 0; i < n; i++)

    for(j = 0; j < m; j++)

    {

      int s = con.nextInt();

      if (s == 1) res += c[i][j];

    }   

    System.out.println(res);

    con.close();

  }

}